package com.study.talk.module.netty.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.vo.GroupInfoVo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.CreateGroupRequest;
import com.study.talk.module.netty.message.response.CreateGroupResponse;
import com.study.talk.module.netty.message.response.InviteGroupResponse;
import com.study.talk.module.service.GroupInfoService;
import com.study.talk.module.service.GroupUserInfoService;
import com.study.talk.module.service.InviteGroupRecordService;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 创建群聊消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class CreateGroupRequestHandler extends SimpleChannelInboundHandler<CreateGroupRequest> {

    @Resource
    private GroupInfoService groupInfoService;

    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Resource
    private UserService userService;

    @Resource
    private InviteGroupRecordService inviteGroupRecordService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CreateGroupRequest msg) throws Exception {
        log.info("创建群聊事件,{}", msg);

        CreateGroupResponse createGroupResponse = new CreateGroupResponse();
        Session session = ctx.channel().attr(Attributes.SESSION).get();
        List<GroupInfo> groupInfoList = groupInfoService.list(new QueryWrapper<GroupInfo>().eq("main_user", session.getUserId()).eq("name", msg.getGroupName()));
        if (groupInfoList != null && groupInfoList.size() > 0) {
            createGroupResponse.setGroupName(msg.getGroupName());
            createGroupResponse.setSuccess(false);
            createGroupResponse.setReason("不能创建同名群！");
            createGroupResponse.setDateTime(msg.getDateTime());
            ctx.channel().writeAndFlush(createGroupResponse);
            return;
        }


        groupInfoService.addGroup(new GroupInfoVo(msg));

        GroupInfo groupInfo = groupInfoService.getOne(new QueryWrapper<GroupInfo>().eq("main_user", session.getUserId()).eq("name", msg.getGroupName()));

        //把群主加入群聊
        groupUserInfoService.addMember(groupInfo.getId(), msg.getMainUser());

        createGroupResponse.setSuccess(true);
        createGroupResponse.setGroupId(groupInfo.getId());
        createGroupResponse.setGroupName(groupInfo.getName());
        createGroupResponse.setDateTime(msg.getDateTime());

        userService.sendMessage(session.getUserId(), createGroupResponse);

        if (msg.getUserList() == null || msg.getUserList().size() == 0) {
            return;
        }

        InviteGroupResponse inviteGroupResponse = new InviteGroupResponse();
        inviteGroupResponse.setGroupId(groupInfo.getId());
        inviteGroupResponse.setGroupName(groupInfo.getName());
        inviteGroupResponse.setInviteUserId(groupInfo.getMainUser());
        inviteGroupResponse.setInviteUserName(session.getUserName());
        inviteGroupResponse.setDateTime(msg.getDateTime());
        for (Integer userId : msg.getUserList()) {
            if (!inviteGroupRecordService.queryHasInviteMember(groupInfo.getId(), msg.getMainUser(), userId)) { //是否已经有邀请的记录了
                inviteGroupRecordService.addInviteMember(groupInfo.getId(), msg.getMainUser(), userId);
            }
            userService.sendMessage(userId, inviteGroupResponse);
        }
    }
}
